﻿/**********************************************************
 * URLConverter.cs
 * Converts the sharepoint url into a relative path
 * **********************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using Microsoft.SharePoint.Client;
using SharePointExplorer.SharePoint;


namespace SharePointExplorer.Classes
{
    public class URLConverter : IValueConverter
    {
        /// <summary>
        /// Converts the sharepoint url into relative path
        /// </summary>
        /// <param name="value">File Explorer or not</param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            File file = null;

            if (value != null)
            {
                file = value as File;
                string WebUrl = SharePointHelper.SharePointClientContext.Url;
                string endStr = file.ServerRelativeUrl.Split('/').LastOrDefault();
                if(endStr.Contains(".jpg") || endStr.Contains(".png"))
                    return string.Concat(WebUrl, file.ServerRelativeUrl);
                else
                    return @"../Images/FileExplorer.png";
            }
            else
            {
                return @"../Images/FileExplorer.png";
            }
            //return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
}
